<?php

if( !defined( 'MEDIAWIKI' ) )
die(-1);

require_once('DatabaseFunctions.php');

#install extension hook
$wgExtensionFunctions[] = "wfLastComments";


function wfLastComments(){
  global $wgRequest, $wgDBprefix, $wgOut, $wgUser, $wgLang ,$IP, $wgScript;;
  $value = $wgRequest->getText( 'title' );
  $values = explode(":", $value);

  //First check if the type of content (url) is the kind to render this module (Main_Page)
  //if ($values[0] == 'Main_Page') {
  if ($value == 'Category:All') {
    $wgOut->setOrder(20);
    $wgOut->switchBocataMode(false);
    $dbr = wfGetDB( DB_SLAVE );


    //fetch the list of users with more comments,
    $sql = "SELECT * FROM ss_file_comments WHERE lang = '{$wgLang->getCode()}' ORDER BY date DESC LIMIT 10";
    $res = $dbr->query( $sql );

    
    $out .= '<div class="home_module" id="lastcomments">';
    $out .= '<h2 >'.wfMsg( 'last_comments' ).'</h2>';
    $out .= '<div id="lastcomments_show">';
    

    while ($row = $dbr->fetchObject( $res )) {
          
           //fetch the song relative to file 
          $sql2 = "SELECT Artist,Title FROM ss_tags WHERE ss_tags.IdFile = $row->id_file AND idfile IS NOT NULL LIMIT 1";
          $res2 = $dbr->query( $sql2 );
          $rowTag = $dbr->fetchObject( $res2 );
          
          
          $avatar = new wAvatar( $row->id_user, 'm' );
          
          $out .= '<div><div><a href="/users/'.$wgUser->whoIs($row->id_user).'">'.$avatar->getAvatarURL().'</a>
				
				<small><a href="/users/'.$wgUser->whoIs($row->id_user).'">'.$wgUser->whoIs($row->id_user).'</a> on '.date("g:i a F j, Y ", strtotime($row->date)).
				'</small></div>
                                <a href="/download/'.wfUrlencode($rowTag->Artist).'/'.wfUrlencode($rowTag->Title).'#'.$row->id_comment.'">
                                <span>'.truncateString($row->comment, 300).'</span></a>
                                </div><br>';
          $dbr->freeResult( $res2 );
    }
    
    $out .= '</div></div>';

    $wgOut->addHtml($out);
    $wgOut->resetOrder();
    
    
    $dbr->freeResult( $res );
    
    
  }
}
